/*
 * Copyright 2019 ConsenSys AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
package org.hyperledger.besu.ethereum.eth.peervalidation;

import static org.assertj.core.api.Assertions.assertThat;

import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions;
import org.hyperledger.besu.ethereum.eth.manager.EthPeer;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestBuilder;
import org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil;
import org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer;
import org.hyperledger.besu.ethereum.eth.manager.peertask.PeerTaskExecutor;
import org.hyperledger.besu.ethereum.eth.manager.peertask.PeerTaskExecutorResponseCode;
import org.hyperledger.besu.ethereum.eth.manager.peertask.PeerTaskExecutorResult;
import org.hyperledger.besu.ethereum.eth.manager.peertask.task.GetHeadersFromPeerTask;
import org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage;
import org.hyperledger.besu.ethereum.eth.messages.EthProtocolMessages;
import org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage;
import org.hyperledger.besu.testutil.DeterministicEthScheduler;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public abstract class AbstractPeerBlockValidatorTest {
  protected PeerTaskExecutor peerTaskExecutor;

  @BeforeEach
  public void beforeTest() {
    peerTaskExecutor = Mockito.mock(PeerTaskExecutor.class);
  }

  abstract AbstractPeerBlockValidator createValidator(
      PeerTaskExecutor peerTaskExecutor, long blockNumber, long buffer);

  @Test
  public void validatePeer_unresponsivePeer() {
    final EthProtocolManager ethProtocolManager =
        EthProtocolManagerTestBuilder.builder()
            .setEthScheduler(
                new DeterministicEthScheduler(
                    DeterministicEthScheduler.TimeoutPolicy.ALWAYS_TIMEOUT))
            .build();
    final long blockNumber = 500;
    final PeerValidator validator = createValidator(peerTaskExecutor, blockNumber, 0);

    final EthPeer peer =
        EthProtocolManagerTestUtil.createPeer(ethProtocolManager, blockNumber).getEthPeer();

    Mockito.when(
            peerTaskExecutor.executeAgainstPeer(
                Mockito.any(GetHeadersFromPeerTask.class), Mockito.eq(peer)))
        .thenReturn(
            new PeerTaskExecutorResult<>(
                Optional.empty(), PeerTaskExecutorResponseCode.TIMEOUT, List.of(peer)));

    final CompletableFuture<Boolean> result =
        validator.validatePeer(ethProtocolManager.ethContext(), peer);

    // Request should timeout immediately
    assertThat(result).isDone();
    assertThat(result).isCompletedWithValue(false);
  }

  @Test
  public void validatePeer_requestBlockFromPeerBeingTested()
      throws ExecutionException, InterruptedException {
    final EthProtocolManager ethProtocolManager = EthProtocolManagerTestBuilder.builder().build();
    final BlockDataGenerator gen = new BlockDataGenerator(1);
    final long blockNumber = 500;
    final Block block = gen.block(BlockOptions.create().setBlockNumber(blockNumber));

    final PeerValidator validator = createValidator(peerTaskExecutor, blockNumber, 0);

    final int peerCount = 24;
    final List<EthPeer> otherPeers =
        Stream.generate(
                () ->
                    EthProtocolManagerTestUtil.createPeer(ethProtocolManager, blockNumber)
                        .getEthPeer())
            .limit(peerCount)
            .toList();
    final EthPeer targetPeer =
        EthProtocolManagerTestUtil.createPeer(ethProtocolManager, blockNumber).getEthPeer();

    Mockito.when(
            peerTaskExecutor.executeAgainstPeer(
                Mockito.any(GetHeadersFromPeerTask.class), Mockito.eq(targetPeer)))
        .thenReturn(
            new PeerTaskExecutorResult<>(
                Optional.of(List.of(block.getHeader())),
                PeerTaskExecutorResponseCode.SUCCESS,
                List.of(targetPeer)));
    otherPeers.forEach(
        (p) ->
            Mockito.when(
                    peerTaskExecutor.executeAgainstPeer(
                        Mockito.any(GetHeadersFromPeerTask.class), Mockito.eq(p)))
                .thenThrow(new RuntimeException("Test failed: wrong peer validated")));
    final CompletableFuture<Boolean> result =
        validator.validatePeer(ethProtocolManager.ethContext(), targetPeer);

    Mockito.verify(peerTaskExecutor)
        .executeAgainstPeer(Mockito.any(GetHeadersFromPeerTask.class), Mockito.eq(targetPeer));
    Mockito.verifyNoMoreInteractions(peerTaskExecutor);
    assertThat(result).isDone();
  }

  @Test
  public void canBeValidated() {
    final BlockDataGenerator gen = new BlockDataGenerator(1);
    final EthProtocolManager ethProtocolManager =
        EthProtocolManagerTestBuilder.builder()
            .setEthScheduler(
                new DeterministicEthScheduler(
                    DeterministicEthScheduler.TimeoutPolicy.ALWAYS_TIMEOUT))
            .build();
    final long blockNumber = 500;
    final long buffer = 10;

    final PeerValidator validator = createValidator(peerTaskExecutor, blockNumber, buffer);
    final EthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 0).getEthPeer();

    peer.chainState().update(gen.hash(), blockNumber - 10);
    assertThat(validator.canBeValidated(peer)).isFalse();

    peer.chainState().update(gen.hash(), blockNumber);
    assertThat(validator.canBeValidated(peer)).isFalse();

    peer.chainState().update(gen.hash(), blockNumber + buffer - 1);
    assertThat(validator.canBeValidated(peer)).isFalse();

    peer.chainState().update(gen.hash(), blockNumber + buffer);
    assertThat(validator.canBeValidated(peer)).isTrue();

    peer.chainState().update(gen.hash(), blockNumber + buffer + 10);
    assertThat(validator.canBeValidated(peer)).isTrue();
  }

  AtomicBoolean respondToBlockRequest(final RespondingEthPeer peer, final Block block) {
    final AtomicBoolean blockRequested = new AtomicBoolean(false);

    final RespondingEthPeer.Responder responder =
        RespondingEthPeer.targetedResponder(
            (cap, p, msg) -> {
              if (msg.getCode() != EthProtocolMessages.GET_BLOCK_HEADERS) {
                return false;
              }
              final GetBlockHeadersMessage headersRequest = GetBlockHeadersMessage.readFrom(msg);
              final boolean isTargetedBlockRequest =
                  headersRequest.blockNumber().isPresent()
                      && headersRequest.blockNumber().getAsLong() == block.getHeader().getNumber();
              if (isTargetedBlockRequest) {
                blockRequested.set(true);
              }
              return isTargetedBlockRequest;
            },
            (cap, p, msg) -> BlockHeadersMessage.create(block.getHeader()));

    // Respond
    peer.respond(responder);

    return blockRequested;
  }
}
